<?php
class Front_Form_Enregistrement extends Jnd_Form
{

    public function __construct( $decorateur = "Formulairejndpro", $options = null )
    {
        parent::__construct($decorateur, $options);
        $this->setElementDecorators(array(
            "$decorateur"
        ), array(), false);
    }

    public function init()
    {
        $this->setName("enregistrement");
        $this->setAction("/accueil/enregistrement");
        $this->setMethod("post");
        $this->addAttribs(array(
            "class" => "cssform"
        ));
        
        $champ = new Zend_Form_Element_Hidden("id");
        $champs[] = $champ;
        
        $mapper = new Back_Model_Mapper_Utilisateurs();
        $table = $mapper->_getTable();
        $options = array(
            "table" => $table->info(Zend_Db_Table::NAME), 
            "field" => $mapper->getDict("pseudo")
        );
        $validator = new Zend_Validate_Db_NoRecordExists($options);
        $champ = new Zend_Form_Element_Text("pseudo");
        $champ->setLabel(__("Pseudo"));
        $champ->setRequired(true);
        $champ->addValidator($validator);
        $champs[] = $champ;
        
        $options = array(
            "table" => $table->info(Zend_Db_Table::NAME), 
            "field" => $mapper->getDict("mail")
        );
        //$validator = new Zend_Validate_Db_NoRecordExists($options);
        $champ = new Zend_Form_Element_Text("mail");
        $champ->setLabel(__("email"));
        //$champ->addValidator($validator);
        $champ->setRequired(true);
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Text("nom");
        $champ->setLabel(__("Nom"));
        $champ->setRequired(true);
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Text("prenom");
        $champ->setLabel(__("Prénom"));
        $champ->setRequired(true);
        $champs[] = $champ;
        
        $champ = new Jnd_Form_Element_DatePicker("dateNaissance");
        $champ->setLabel(__("Date de naissance"));
        $champ->setRequired(true);
        $champ->setAttribs(array(
            "changeYear" => true, 
            "changeMonth" => true, 
            "yearRange" => "c-40:c+28", 			
            "numberOfMonths" => 3, 
            "showButtonPanel" => true, 
            "defaultDate" => '-30y'
        ));
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Password("motDePasse");
        $champ->setLabel(__("Mot de passe"));
        $champ->setRequired(true);
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Password("confirmation");
        $champ->setLabel(__("Confirmation"));
        $champ->setRequired(true);
        $champs[] = $champ;
        
        $this->addElements($champs);
    }

    /**
     *
     * @see Zend_Form::isValid()
     */
    public function isValid( $data )
    {
        $valid = parent::isValid($data);
        if (! $valid) {
            return $valid;
        }
        
        $mdp = $this->getElement("motDePasse")->getValue();
        $mdpConfirm = $this->getElement("confirmation")->getValue();
        
        if ($mdp != $mdpConfirm) {
            $this->getElement("motDePasse")->addError(__("La confirmation et le mot de passe ne sont pas identiques"))->setValue($mdp);
            return false;
        }
        return true;
    }
}

